package com.foreknow.elm.dao.Impl;

import com.foreknow.elm.dao.FoodDao;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FoodDaoImpl implements FoodDao {
    private Connection connection;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public List<Food> listFoodByBusinessId(Integer businessId) throws SQLException {
        List<Food> list=new ArrayList<>();
        String sql="select * from food where businessId=?";
        try {
            connection= DBUtil.getConnection();
            pst=connection.prepareStatement(sql);
            pst.setInt(1,businessId);
            rs= pst.executeQuery();
            while (rs.next()){
                Food food=new Food();
                food.setFoodId(rs.getInt("FoodId"));
                food.setFoodName(rs.getString("FoodName"));
                food.setFoodExplain(rs.getString("FoodExplain"));
                food.setFoodImg(rs.getString("FoodImg"));
                food.setFoodPrice(rs.getDouble("FoodPrice"));
                food.setBusinessId(rs.getInt("BusinessId"));
                food.setRemarks(rs.getString("Remarks"));
                list.add(food);
            }
        }finally {
            DBUtil.close(pst);
        }
        return list;
    }

    public static void main(String[] args) {
//        FoodDao foodDao=new FoodDaoImpl();
//        try {
//            List<Food> list= foodDao.listFoodByBusinessId(10001);
//            for (int i=0;i<list.size();i++){
//                System.out.println(list.get(i).getFoodName());
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
    }
}
